using System;
using System.Linq;
using System.Collections.Generic;

namespace WiMo.Games.Drawables
{
    public abstract class SpriteShapeBase : SpriteBase, ISpriteShape
    {
        public SpriteShapeBase()
        {
            Points = new List<Location>();
        }

        public virtual ShapeType ShapeType { get; set; }
        public virtual Rectangle Rect
        {
            get
            {
                if (Origin == Origin.Center)
                    return new Rectangle(Position.Point.X - (Size.Width >> 1), Position.Point.Y - (Size.Height >> 1), Size.Width, Size.Height);

                return new Rectangle(Position.Point.X, Position.Point.Y, Size.Width, Size.Height);
            }
            set
            {
                if(UnscaledRect != value)
                {                    
                    Size = new Size(value.Width, value.Height);

                    if (Origin == Origin.Center)
                        Position = new PointF(value.Left + (Size.Width >> 1), value.Top + (Size.Height >> 1));
                    else
                        Position = new PointF(value.Left, value.Top);

                    Invalidate();
                }
            }
        }

        private Rectangle UnscaledRect
        {
            get
            {
                return new Rectangle(UnscaledPosition.X, UnscaledPosition.Y, UnscaledSize.Width, UnscaledSize.Height);
            }
        }

        private int _borderRadius;
        public virtual int BorderRadius
        {
            get { return _borderRadius; }
            set
            {
                if (_borderRadius != value)
                {
                    Invalidate();
                    _borderRadius = value;
                }
            }
        }

        private Color _fillColor;
        public virtual Color FillColor
        {
            get
            {
                return _fillColor;
            }
            set
            {
                if(_fillColor != value)
                {
                    Invalidate();
                    _fillColor = value;
                }
            }
        }
        private Color _borderColor;
        public virtual Color BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                if(_borderColor != value)
                {
                    Invalidate();
                    _borderColor = value;
                }
            }
        }
        private int _borderWidth;
        public virtual int BorderWidth
        {
            get
            {
                return _borderWidth;
            }
            set
            {
                if(_borderWidth != value)
                {
                    Invalidate();
                    _borderWidth = value;
                }
            }
        }
        public virtual IGeometry Shape { get; set; }
        public virtual List<Location> Points { get; set; }
        private ITexture _texture;
        public virtual ITexture Texture
        {
            get
            {
                return _texture;
            }
            set
            {
                if(_texture != value)
                {
                    Invalidate();
                    _texture = value;
                }
            }
        }
        public virtual void AddPoint(Location point)
        {
            Invalidate();
            var scaledPoint = new Location((int)(Scale.X * point.X), (int)(Scale.Y * point.Y));

            if (scaledPoint.X < BoundingRect.X)
                _boundingRectOverride.X = scaledPoint.X;

            if (scaledPoint.Y < BoundingRect.Y)
                _boundingRectOverride.Y = scaledPoint.Y;

            if (scaledPoint.X > BoundingRect.Size.Width)
            {
                _boundingRectOverride.Width = scaledPoint.X;
                _boundingRectOverride.Height = _boundingRectOverride.Height;
            }

            if (scaledPoint.Y > BoundingRect.Size.Height)
            {
                _boundingRectOverride.Width = _boundingRectOverride.Width;
                _boundingRectOverride.Height = scaledPoint.Y;
            }

            Points.Add(scaledPoint);
        }

        public void CalcBoundingRect()
        {
            if (Points.Count() > 0)
            {
                var maxX = Points.Max(p => p.X);
                var maxY = Points.Max(p => p.Y);

                _size = new Size(Convert.ToSingle(maxX) / Scale.X, Convert.ToSingle(maxY) / Scale.Y);
            }
        }

        public void SetTexture(IContentManager contentManager, string name)
        {
            Texture = contentManager.GetTexture(name);
        }
    }
}
